import 'package:flutter_architecture/constant/base_state_enum.dart';
import 'package:flutter_architecture/models/index.dart';
import 'package:flutter_architecture/utils/http_util.dart';
import 'package:flutter_architecture/viewmodels/base_view_model.dart';
import 'package:flutter_architecture/constant/service_url_constants.dart';

/// 用户 ViewModel
/// @author: dingwen
/// @date: 2020/12/30

class UserViewModel extends BaseViewModel {
  List<UserModel> userList = [];

  @override
  fetchData({data, queryParams}) {
    state = BaseState.LOADING;
    Http.request(user_list_url,
        method: Http.POST,
        queryParams: queryParams,
        data: data, success: (resultModel) {
      if (resultModel.data != null) {
        (resultModel.data as List).forEach((user) {
          userList.add(UserModel.fromJson(user));
        });
        state = BaseState.CONTENT;
        notifyListeners();
      } else {
        state = BaseState.EMPTY;
        notifyListeners();
      }
    }, fail: (reason, code) {
      state = BaseState.FAIL;
      notifyListeners();
    }, after: () {});
  }
}
